package lab02.test23;

import sun.util.resources.LocaleData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class Test23 {
    public static void main(String[] args) {
        //编写Java应用程序，从命令行输入你的生日信息
        //计算你的生日还有多少天
        // 你的生日是星期几。


        //保存生日信息
        String birthday;

        System.out.println("Please input your birthday:");
        Scanner scanner = new Scanner(System.in);
        birthday = scanner.nextLine();

        /**
         *
         * 2017-11-20  ->long time1  毫秒
         * 2017-11-10  ->long time2  毫秒
         *
         * (time1-time2)/(24*60*60*1000)
         */
        long time1 = 0;
        long time2 = 0;


        /**
         * 实例化
         * Student chen = new Student();
         * Study study = new StudyImpl();
         * Calendar calendar = Calendar.getInstance();
         *
         * 字符串---simpleDateFormat-->Date---calendar---->long
         */


        try{
            //如何将字符串转化为日期：Date对象
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");     //将输入的字符串格式化
            Date birthDate = simpleDateFormat.parse(birthday);                                  //解析字符串为时间

            //如何将一个时间转换为long
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(birthDate);            //设置生日日期
//            calendar.get(calendar.DAY_OF_WEEK);     //返回一个日期为星期几
            time1 = calendar.getTimeInMillis() ;     //将日期转化为long类型的数字



            Date currentDate = simpleDateFormat.parse("2017-11-10");
            calendar.setTime(currentDate);
            time2 = calendar.getTimeInMillis() ;
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            long days = (time1 - time2)/(24*60*60*1000);
            System.out.println("距离生日还有" + days + "天!");
        }
    }
}
